#!/usr/bin/env php
<?php
define('APPLICATION_ENV','production');
// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (	('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';
/** Zend_Application */
//require_once 'Doctrine.php';

//require 'Zend/Loader.p
require_once 'Zend/Application.php';

$application = new Zend_Application(APPLICATION_ENV,
    array(
  //      'bootstrap'=>array('path'=>APPLICATION_PATH.'/Bootstrap.php'),
        'autoloadernamespaces' => array('Zend', 'Sfs')
    ));
$application->bootstrap();
//Zend_Loader::registerAutoload('Zend_Loader');

// Create application, bootstrap, and run
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini','production');
//echo APPLICATION_PATH.'/configs/application.ini';
$cc =  $config->toArray();

// 取得版號
$core_rev = trim(file_get_contents(APPLICATION_PATH.'/configs/version'));
// 更新站台
$url = 'http://localhost/svn_export/svn_export.php';//?rev='.$core_rev;
// 下載暫存 壓縮檔案
$temp_zip = '/tmp/cc.zip';
// 解壓縮目錄
$temp_data_path = '/tmp/cc';
$http_client = new Zend_Http_Client();
$http_client->setConfig(array(
    'maxredirects' => 0,
    'timeout'      => 100));
$http_client->setUri($url);
$http_client->setParameterGet('rev',$core_rev);
$res = $http_client->request();

file_put_contents($temp_zip,$res->getBody());

$unzip = new Sfs_Filesystem_Unzip();

$unzip->extractZip($temp_zip,$temp_data_path);

if (is_file($temp_data_path.'/sfs_log.php')) {
    $log = unserialize(file_get_contents($temp_data_path.'/sfs_log.php'));
    print_r($log);
}
